/**
 * 
 */
package games;

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

import javax.swing.JFrame;

/**
 * @author Owner
 *
 */
public class Runner {
	JFrame frame;
	PrintStream log;
	Game game;

	String name;
	String id;
	
	boolean gameCompleted=false;
	
	/**
	 * 
	 */
	public Runner(String name,String id) {
		this.name=name;
		this.id=id;
		
		try {
			log=new PrintStream(new File("gameLog.txt"));
		} catch(FileNotFoundException e) {
			System.err.println("Cannot find file");
			e.printStackTrace();
		}
		log.println("Subect: "+name+"\tid: "+id);
		log.flush();
	}
	
	public void runFirstGame() {
		frame=new JFrame();
		frame.setLayout(new BorderLayout());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		game=new WaitGame(this);
    game.init();
    frame.add(BorderLayout.CENTER,game);
		frame.pack();
		frame.setVisible(true);
		frame.setSize(600,500);
		
		while(!gameCompleted);
		

    runAgain(new InstructionMovie(this,"filler.mov"));
		runAgain(new Instructions(this,"fillerGuess.html"));
		runAgain(new BeautyContest(this));
		runAgain(new InstructionMovie(this,"filler.mov"));
		runAgain(new Instructions(this,"fillerImpulse.html"));
		
		runAgain(new Centipede(this));
		runAgain(new BeautyContest(this));
		runAgain(new Dictator(this));
		runAgain(new PrisonersDilemma(this));
		runAgain(new Impulse(this));
		runAgain(new Centipede(this));
		
		runLastGame();
		
	}
	
	public void runAgain(Game next) {
		gameCompleted=false;
		frame.remove(game);
		
    frame.add(game=next);
    game.init();
    game.start();
    game.revalidate();

		while(!gameCompleted);
	}
	
	public void runLastGame() {
		runAgain(new WaitGame(this,false));
		
		while(!gameCompleted);
		System.exit(0);
	}
	
	public void runGame() {
    game=new InstructionMovie(this,"filler.mov");
    game.init();
    frame.add(BorderLayout.CENTER,game);
		frame.pack();
		frame.setVisible(true);
		frame.setSize(600,500);


		
    
		/*gameCompleted=false;
		frame.remove(game);
		
		
    game=new Instructions(this,"Instructions.html");
    frame.add(game);
    game.init();
    game.revalidate();

		while(!gameCompleted);
		gameCompleted=false;
    //frame.remove(in);

    game=new Impulse(this);
		//frame.add(BorderLayout.CENTER,game);
		//f.pack();
		//f.setVisible(true);
		//f.setSize(600,500);

		game.init();
		game.start();

    frame.repaint();
		
		while(!gameCompleted);
		gameCompleted=false;
    //frame.remove(game1);
		
		game=new InstructionMovie(this,"filler.mov");
    game.init();

    frame.repaint();
    //frame.add(BorderLayout.CENTER,game);
		//frame.pack();
		//frame.setVisible(true);
		//frame.setSize(600,500);*/
	}
	
  /**
   * Controls progress of the game. After this call, actions by the players will
   * be processed until suspend() is called, or until finished() is called.
   *
  public synchronized void proceed() {
    if (gameCompleted) {
      System.err.println("cannot restart completed game");
      return;
    }
    //gameValue = "proceeding";
    //if (clock.suspended()) {
    //  clock.proceed();
    //  sessionLog.println("game proceeding " + System.currentTimeMillis());
    //} else
      System.err.println("game still proceeding");
    //manager.bulletin(getTitle() + " in progress");
  }

  /**
   * Controls progress of the game. After this call, actions by the players will
   * be ignored, but if proceed() is called subsequently, actions will be
   * re-enabled.
   *
  public synchronized void suspend() {
    //gameValue = "suspended";
    //if (!clock.suspended()) {
    //  clock.suspend();
      //sessionLog.println("game suspended " + System.currentTimeMillis());
    //}
    //System.err.println("game suspended");
    //manager.bulletin(getTitle() + " suspended");
  }

  /**
   * called by subclass or by the supervisor to terminate the game. It will
   * announce to the supervisor that the game is complete, and the subclass is
   * expected to stop all future operations from clients.
   * 
   * @param message
   *          a string that will be relayed to the supervisor.
   */
  public synchronized void finished(String message, int time) {
    gameCompleted=true;
    //suspend();
    //System.err.println("message from game Supervisor: " + message);
    //sessionLog.println(message + "  " + System.currentTimeMillis());
    Toolkit.getDefaultToolkit().beep();
    //gameValue = message;
    //showPayoffs(time);
    // gameLog.println("finished "+getSource());
  }
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Runner().runFirstGame();

	}
	
	public Runner() {
		this("test","test");
	}

}
